function [im,map] = my_image_read(imgPath,format)
%{
	This function gets an image's path and a format:
		1 - Intensity Image
		2 - RGB Image
		3 - Indexed Image
	and returns the image at the given path at the given format.
	note that format 3 returns 2 values, the image and the color map while
	formats 1 and 2 returns only image.
	
	assumptions: the format is correct.
	In case of any error and exception will be thrown and the flow will halt
%}
if (nargin~=2)
 throw(MException('MATLAB:nargchk:notEnoughInputs','the function my_image_read should get 2 input arguments'));
end

try

	%this following 2 commands will throw an exception in case of invalid path or invalid file's content.
	[myImg,myMap] = imread(imgPath); 
	k = imfinfo(imgPath);

	%determine the type of the given image and accordingly convert it to the relevant type 
	switch k.ColorType
	case('indexed')%Indexed image
		switch format
		case (1) %Intensity image
			im = ind2gray(myImg,myMap);
		case (2) %RGB image
			im = ind2rgb(myImg,myMap);
		case (3) %Indexed image	
			im = myImg;
			map = myMap;
		end
	case('truecolor')%RGB Image
		switch format
		case (1) 
			im = rgb2gray(im2double(myImg));
		case (2) 
			im = im2double(myImg);
		case (3) 
			[im,map] = rgb2ind(myImg,256);
		end
	case('grayscale')%Grayscale or Bland&White image
		switch format
		case (1) 
			im = im2double(myImg);
		case (2) 
			im=(im2double(cat(3,myImg,myImg,myImg)));
		case (3) 
			[im, map] = gray2ind(myImg,256);
		end
	end

catch exception
	throw(MException('MATLAB:RunTimeError','Invalid Path or Invalid image file content'));
end
